package com.bestow.lease.service.impl;

import com.bestow.lease.service.IHlsCusExcelFileService;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.RegionUtil;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.Objects;

@Transactional(rollbackFor = Exception.class)
@Service
public class HlsCusExcelFileServiceImpl implements IHlsCusExcelFileService {

    private final String FONT_NAME_FS = "仿宋";
    private final String DEFULT_VALUE = "无";
    private DecimalFormat df = new DecimalFormat("000,"+ "000.00");


    @Override
    public String createPlanExcelFile(Long planId) {

        // 第一步，创建一个Workbook，对应一个Excel文件 HSSFWorkbook 2003 XSSFWorkbook2007
        XSSFWorkbook wb = new XSSFWorkbook();

        // 第二步，在workbook中添加一个sheet,对应Excel文件中的sheet
        XSSFSheet sheet = wb.createSheet("融资租赁项目方案");

        // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
        XSSFRow row = sheet.createRow(0);

        // 第四步，创建单元格，并设置值表头 设置表头居中
        //字体
        XSSFFont font_11 = this.getFont(wb,(short) 11,FONT_NAME_FS);
        XSSFFont font_16 = this.getFont(wb,(short) 16,FONT_NAME_FS);

        //样式
        XSSFCellStyle cellStyle1 = this.getCellStyle(wb, VerticalAlignment.CENTER, HorizontalAlignment.CENTER,null,null,false);

        XSSFCellStyle cellStyle2 = this.getCellStyle(wb, VerticalAlignment.BOTTOM, HorizontalAlignment.CENTER,null,font_11,false);

        XSSFCellStyle cellStyle3 = this.getCellStyle(wb, VerticalAlignment.CENTER, HorizontalAlignment.CENTER,null,font_16,true);

        XSSFColor color = new XSSFColor(new java.awt.Color((byte) (0xff & 198), (byte) (0xff & 224), (byte) (0xff & 180)));
        XSSFCellStyle cellStyle4 = this.getCellStyle(wb, VerticalAlignment.CENTER, HorizontalAlignment.CENTER,color,null,true);

        XSSFColor color2 = new XSSFColor(new java.awt.Color((byte) (0xff & 226), (byte) (0xff & 239), (byte) (0xff & 218)));
        XSSFCellStyle cellStyle5 = this.getCellStyle(wb, VerticalAlignment.CENTER, HorizontalAlignment.LEFT,color2,null,true);


        // 第五步，创建行列开始写入
        XSSFRow rowCheck;//行
        XSSFCell cellCheck;//列
        int rownum = 1;

        rowCheck = sheet.createRow(rownum);//1
        rowCheck.setHeight((short) 700);
        cellCheck = rowCheck.createCell(1);
        this.writeCellCheck(cellCheck, CellType.STRING, cellStyle1, null);



        return null;
    }

    @Override
    public String createBaoliPlanExcelFile(Long planId) {
        return null;
    }

    /**
     * 设置Excel字体
     */
    private XSSFFont getFont(XSSFWorkbook wb,short height,String fontName){
        String message = "XSSFWorkbook must not be null!";
        Objects.requireNonNull(wb, () -> message);
        XSSFFont font = wb.createFont();
        font.setFontHeightInPoints(height);//设置字体大小
        font.setFontName(fontName);
        font.setBold(true);//是否加粗 对应旧版本setBoldweight(HSSFFont.BOLDWEIGHT_BOLD)

        return font;
    }

    /**
     * 设置Excel样式
     */
    private XSSFCellStyle getCellStyle(XSSFWorkbook wb, VerticalAlignment verticalAlignment,HorizontalAlignment horizontalAlignment,XSSFColor color, Font font,Boolean setBorderFlag) {
        String message = "XSSFWorkbook must not be null!";
        Objects.requireNonNull(wb, () -> message);
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cellStyle.setWrapText(true);//自动换行
        cellStyle.setVerticalAlignment(verticalAlignment);//内容沿y轴的对齐方式 对应旧版本HSSFCellStyle.VERTICAL_CENTER
        cellStyle.setAlignment(horizontalAlignment);//表格内容水平垂直居中 对应旧版本HSSFCellStyle.ALIGN_CENTER
        if(setBorderFlag){
            //设置边框
            cellStyle.setBorderBottom(BorderStyle.THIN);//对应旧版本HSSFCellStyle.BORDER_THIN
            cellStyle.setBorderLeft(BorderStyle.THIN);
            cellStyle.setBorderRight(BorderStyle.THIN);
            cellStyle.setBorderTop(BorderStyle.THIN);
        }


        return cellStyle;
    }

    /**
     * 写入Excel行记录
     */
    private void writeCellCheck(XSSFCell cellCheck, CellType cellType, CellStyle style, String value) {
        cellCheck.setCellType(cellType);
        cellCheck.setCellStyle(style);
        cellCheck.setCellValue(value);
    }

    /**
     * 设置Excel页脚
     */
    private void setExcelFooterName(String customExcelFooterName, int setExcelFooterNumber, XSSFWorkbook wb) {
        wb.setSheetName(setExcelFooterNumber, customExcelFooterName);
    }

    /**
     * 合并竖列单元格设置边框边框
     */
    private static void setVerRegionBorder(int a, int b, int c, int d, XSSFSheet sheet, XSSFWorkbook wb) {
        //合并单元格区域只有一个单元格时，不合并
        if (a == b && c == d) {
            return;
        }
        CellRangeAddress cra;
        //CellRangeAddress(起始行号，终止行号， 起始列号，终止列号）
        cra = new CellRangeAddress((short) a, (short) b, (short) c, (short) d);
        sheet.addMergedRegion(cra);
        RegionUtil.setBorderBottom(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderLeft(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, cra, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, cra, sheet);
    }

    /**
     * 生成文件到指定目录
     */
    private void writeExcel(XSSFWorkbook wb, String filePath, String fileName) {
        File file = new File(filePath);
        OutputStream stream = null;
        try {
            stream = new FileOutputStream(new File(file, fileName));
            wb.write(stream);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
